package com.ld.security.server.admin.modules.ge.service.impl;


import com.ld.security.common.mybatis.base.service.ISuperServiceImpl;
import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.server.admin.modules.ge.constant.GeProjectUserRelConstant;
import com.ld.security.server.admin.modules.ge.entity.GeProjectUserRelEntity;
import com.ld.security.server.admin.modules.ge.mapping.GeProjectUserRelMapping;
import com.ld.security.server.admin.modules.ge.mapping.IGeProjectMapping;
import com.ld.security.server.admin.modules.ge.service.IGeProjectUserRelService;
import com.ld.security.server.admin.modules.ge.vo.ProjectUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GeProjectUserRelServiceImpl  extends ISuperServiceImpl<GeProjectUserRelEntity>
implements
        IGeProjectUserRelService {

        @Autowired
        GeProjectUserRelMapping geProjectUserRelMapping;

        @Autowired
        IGeProjectMapping  iGeProjectMapping;

        @Cacheable(cacheNames = "def",key = "#userId")
        @Override
        public GeProjectUserRelEntity getDef(String userId) {
                GeProjectUserRelEntity e =   this.newInstance();
                e.setStatus(GeProjectUserRelConstant.status.run);
                e.setUserId(userId);
                GeProjectUserRelEntity val =  this.findOne(e);
               return val;
        }

        @CacheEvict(cacheNames = "def",key = "#userId")
        /**
         * 设置默认值
         * @param
         * @return
         */
        @Override
        public int useProjectt(String id ,String userId) {

                GeProjectUserRelEntity relEntity =   this.getDef(userId);
                if(relEntity != null){
                        relEntity.setStatus(GeProjectUserRelConstant.status.await);
                        geProjectUserRelMapping.updateById(relEntity);
                }


                GeProjectUserRelEntity e =  this.newInstance();
                e.setId(id);
                e.setStatus(GeProjectUserRelConstant.status.run);
               return geProjectUserRelMapping.updateById(e);
        }

        @Override
        public List<ProjectUserVo> getProject(String userId) {
                ProjectUserVo q = new ProjectUserVo();
                q.setUserId(userId);
                return JdbcUtil.all(q);
        }
}
